/**
 * SrvUtilityPortlets -  http://code.google.com/p/alui-toolbox/
 * Copyright (c) 2008-2009
 * by Fabien Sanglier
 * 
 * This file is part of SrvUtilityPortlets.
 * 
 * SrvUtilityPortlets is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SrvUtilityPortlets is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SrvUtilityPortlets.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEA.ALI.Web.UI;
using com.plumtree.server;
using AluiServerLib.Helper;

namespace ALUIServerUtilities
{
    public partial class WebServiceChanger : ALIPortletPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            portletTreeLink.Text = WebHelper.CreateTreePickerTag(PT_CLASSIDS.PT_GADGET_ID, "Pick Portlets", "Portlets", "Pick Portlets", "PortletsPickerSubmit" + m_portletRequest.GetPortletID());
            portletTemplateTreeLink.Text = WebHelper.CreateTreePickerTag(PT_CLASSIDS.PT_GADGETTEMPLATE_ID, "Pick Portlet Templates", "Portlet Templates", "Pick Portlet Templates", "PortletTemplatesPickerSubmit" + m_portletRequest.GetPortletID());
            wsTreeLink.Text = WebHelper.CreateTreePickerTag(PT_CLASSIDS.PT_WEBSERVICE_ID, "Pick Webservices", "Webservices", "Pick Webservices", "WSPickerSubmit" + m_portletRequest.GetPortletID());

            if (!Page.IsPostBack)
            {
            }
        }

        protected void cmdSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string[] portletids = (!"".Equals(txtPortlet.Text.Trim())) ? txtPortlet.Text.Trim().Split(',') : null;
                string[] portlettplids = (!"".Equals(txtPortletTemplate.Text.Trim())) ? txtPortletTemplate.Text.Trim().Split(',') : null;

                int targetWebServiceId = 0;
                try
                {
                    targetWebServiceId = int.Parse(txtTargetWebservice.Text);
                }
                catch (Exception exc)
                {
                    targetWebServiceId = 0;
                }

                if (targetWebServiceId > 0)
                {
                    IPTSession session = NativeApplicationMgr.GetInstance().GetNativeSession(m_portletRequest.GetLoginToken());
                    IPTObjectManager webServiceMgr = session.GetWebServices();
                    IPTWebService webservice = null;
                    try
                    {
                        webservice = (IPTWebService)webServiceMgr.Open(targetWebServiceId, false);
                    }
                    catch (Exception exc)
                    {
                        webservice = null;
                    }

                    if (null != webservice)
                    {
                        //do the portlets first
                        IPTObjectManager portletMgr = session.GetGadgets();
                        IPTGadget portlet = null;
                        int portletid = 0;
                        if (null != portletids)
                        {
                            foreach (string strPortletId in portletids)
                            {
                                Response.Write("<div>");
                                Response.Write("<font color=\"green\">Transforming Portlet " + strPortletId + "...</font>");

                                try
                                {
                                    portletid = int.Parse(strPortletId);
                                }
                                catch (Exception exc)
                                {
                                    portletid = 0;
                                }

                                if (portletid > 0)
                                {
                                    try
                                    {
                                        portlet = (IPTGadget)portletMgr.Open(portletid, true);
                                        portlet.SetWebServiceID(webservice.GetObjectID());
                                        portlet.Store();
                                        Response.Write("<font color=\"green\">...Portlet " + strPortletId + " Successfully Transformed!</font>");
                                    }
                                    catch (Exception exc)
                                    {
                                        Response.Write("<br /><font color=\"red\">An error Occured.</font><br />");
                                        Response.Write("<br /><font color=\"red\">" + exc + "</font><br />");
                                        portlet = null;
                                    }
                                    finally
                                    {
                                        if (null != portlet && portlet.GetLockState() == PT_LOCKSTATES.PT_LOCKED)
                                            portlet.UnlockObject();

                                        portlet = null;
                                    }
                                }
                                else
                                {
                                    Response.Write("<font color=\"red\">Could not parse Portlet ID " + strPortletId + ". Must be a valid number.</font>");
                                }
                                Response.Write("</div>");
                            }
                            portletMgr = null;
                            Response.Write("<br><font color=\"green\">Portlet Operation Successfull!!</font>");
                        }

                        //do the portlet templates after
                        IPTObjectManager portletTemplateMgr = session.GetGadgetTemplates();
                        IPTGadgetTemplate portletTemplate = null;
                        int portlettemplateid = 0;
                        if (null != portlettplids)
                        {
                            foreach (string strPortletTemplateId in portlettplids)
                            {
                                Response.Write("<div>");
                                Response.Write("<font color=\"green\">Transforming Portlet Template " + strPortletTemplateId + "...</font>");

                                try
                                {
                                    portlettemplateid = int.Parse(strPortletTemplateId);
                                }
                                catch (Exception exc)
                                {
                                    portlettemplateid = 0;
                                }

                                if (portlettemplateid > 0)
                                {
                                    try
                                    {
                                        portletTemplate = (IPTGadgetTemplate)portletTemplateMgr.Open(portlettemplateid, true);
                                        portletTemplate.SetWebServiceID(webservice.GetObjectID());
                                        portletTemplate.Store();
                                        Response.Write("<font color=\"green\">...Portlet Template " + strPortletTemplateId + " Successfully Transformed!</font>");
                                    }
                                    catch (Exception exc)
                                    {
                                        Response.Write("<br /><font color=\"red\">An error Occured.</font><br />");
                                        Response.Write("<br /><font color=\"red\">" + exc + "</font><br />");
                                        portlet = null;
                                    }
                                    finally
                                    {
                                        if (null != portletTemplate && portletTemplate.GetLockState() == PT_LOCKSTATES.PT_LOCKED)
                                            portletTemplate.UnlockObject();

                                        portletTemplate = null;
                                    }
                                }
                                else
                                {
                                    Response.Write("<font color=\"red\">Could not parse Portlet Template ID " + strPortletTemplateId + ". Must be a valid number.</font>");
                                }
                                Response.Write("</div>");
                            }
                            portletTemplateMgr = null;
                            Response.Write("<br><font color=\"green\">Portlet Template Operation Successfull!!</font>");
                        }
                    }
                    else
                    {
                        Response.Write("<br><font color=\"red\">Webservice " + targetWebServiceId + " Not Found!!</font>");
                    }
                }
                else
                {
                    Response.Write("<br><font color=\"red\">Webservice Id " + targetWebServiceId + " is not formatted right.It must be a number.</font>");
                }

                pnlWebServiceChanger.Visible = false;
                Response.Write("<br><br><a href=\"pt://return\">Return To Portal</a>");
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            ClientScript.RegisterClientScriptBlock(Page.GetType(), "PortletsPickerSubmit", WebHelper.CreateTreePickerJS("PortletsPickerSubmit" + m_portletRequest.GetPortletID(), txtPortlet.ClientID, ","), true);
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "PortletTemplatesPickerSubmit", WebHelper.CreateTreePickerJS("PortletTemplatesPickerSubmit" + m_portletRequest.GetPortletID(), txtPortletTemplate.ClientID, ","), true);
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "WSPickerSubmit", WebHelper.CreateTreePickerJS("WSPickerSubmit" + m_portletRequest.GetPortletID(), txtTargetWebservice.ClientID, ","), true);
        }
    }
}
